<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program;
if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2007
sirfragalot@users.sourceforge.net
**********************************************************************************/
/*****
*	BIBTEXIMPORTSTAGE1: BibTeX STAGE1 import class
*
*	$Header: /cvsroot/wikindx/wikindx3/core/file/import/BIBTEXIMPORTSTAGE1.php,v 1.17 2007/10/21 17:04:39 sirfragalot Exp $
*****/
class BIBTEXIMPORTSTAGE1
{
// Constructor
	function BIBTEXIMPORTSTAGE1($db, $vars)
	{
		$this->db = $db;
		$this->vars = $vars;
		include_once("core/session/SESSION.php");
		$this->session = new SESSION();
		include_once("core/file/BIBTEXCONFIG.php");
		$this->config = new BIBTEXCONFIG();
		include_once("core/messages/MESSAGES.php");
		$this->messages = new MESSAGES();
		include_once("core/group/GROUP.php");
		$this->group = new GROUP($this->db);
		include_once("core/file/TAG.php");
		$this->tag = new TAG($this->db);
		include_once("core/html/MISC.php");
		include_once("core/html/FORM.php");
		include_once("core/html/TABLE.php");
		include_once("core/file/import/IMPORTCOMMON.php");
		$this->common = new IMPORTCOMMON($this->db);
	}
// If $paste == TRUE, this is an ordinary user cut 'n' pasting bibtex entries into a textarea box.
	function init($paste, $message = FALSE)
	{
		$this->paste = $paste;
// Load config arrays
		$this->config->bibtex();
// Load groups
		$groups = $this->group->grabAll();
		
		$pString = $message ? $message : FALSE;
		if(!$this->paste)
		{
			$pString .= MISC::p($this->messages->text("import", "warning"));
			$pString .= MISC::p($this->messages->text("import", "introBibtex"));
// Load tags
			$tags = $this->tag->grabAll();
			$pString .= TABLE::tableStart(FALSE, 0, 0, 0, "");
			$pString .= TABLE::trStart();
			$pString .= TABLE::td(FORM::textInput($this->messages->text("import", "tag"), 
				"import_tag", FALSE, 50, 255), FALSE, "", "bottom");
			if($tags)
			{
// add 0 => IGNORE to tags array
				$temp[0] = $this->messages->text("misc", "ignore");
				foreach($tags as $key => $value)
					$temp[$key] = $value;
				$tags = $temp;
				$sessionTag = $this->session->issetVar('import_tagId') ? 
					$this->session->getVar('import_tagId') : FALSE;
				if($sessionTag)
					$element = FORM::selectedBoxValue(FALSE, 'import_tagId', $tags, 5);
				else
					$element = FORM::selectFBoxValue(FALSE, 'import_tagId', $tags, 5);
				$pString .= TABLE::td($element);
			}
			$pString .= TABLE::trEnd();
			$pString .= TABLE::tableEnd();
			$pString .= MISC::br() . "&nbsp;" . MISC::br();
		}
		else if(!$this->session->getVar("setup_superadmin")) // Admin can paste unlimited entries
			$pString .= MISC::p($this->messages->text("import", "pasteBibtex", 
			' ' . $this->session->getVar('setup_maxPaste') . " "));
		$pString .= TABLE::tableStart(FALSE, 0, 10, 0, "");
		$pString .= TABLE::trStart();
		$groupTd = FALSE;
		if(sizeof($groups) > 1)
		{
			$td = MISC::p($this->messages->text("import", "categoryPrompt"));
			if($sessionGroups = $this->session->getVar('import_groups'))
			{
				$sGroups = explode(",", $sessionGroups);
				$element = FORM::selectedBoxValueMultiple($this->messages->text("import", 
					"category"), 'import_groups', $groups, $sGroups, 5);
			}
			else
				$element = FORM::selectFBoxValueMultiple($this->messages->text("import", 
					"category"), 'import_groups', $groups, 5);
			$pString .= TABLE::td($td . $element . MISC::br() . $this->messages->text("hint", "multiples"));
			$groupTd = TRUE;
		}
		if($bibs = $this->common->bibliographySelect())
			$pString .= TABLE::td($bibs, FALSE, "left", "bottom");
		if($this->paste)
		{
			$pString .= TABLE::trEnd();
			$pString .= TABLE::trStart();
			$pString .= TABLE::td(MISC::br() . "&nbsp;" . MISC::br() . FORM::textareaInput(FALSE, 
				"import_paste", FALSE, 80, 20), FALSE, "left", "bottom");
		}
		else
			$pString .= TABLE::td(FORM::fileUpload($this->messages->text("import", "file"), 
				"import_file", 30), FALSE, "left", "bottom");
		$pString .= TABLE::trEnd();
		$pString .= TABLE::trStart();
		if($groupTd)
			$pString .= TABLE::td("&nbsp;");
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		$pString .= MISC::br() . "&nbsp;" . MISC::br();
		$pString .= TABLE::tableStart(FALSE, 0, 10, 0, "");
		$pString .= TABLE::td($this->messages->text("import", "importDuplicates") . "&nbsp;&nbsp;" . 
			FORM::checkbox(FALSE, 'import_importDuplicates'));
//		$pString .= TABLE::trStart();
		$td = $this->messages->text("import", "storeRawBibtex");
//		$pString .= TABLE::trEnd();
//		$pString .= TABLE::trStart();
		$pString .= TABLE::td($td . "&nbsp;&nbsp;" . 
			$this->messages->text("import", "storeRawLabel") . "&nbsp;&nbsp;" . 
			FORM::checkbox(FALSE, 'import_raw'));
//		$pString .= TABLE::trEnd();
//		$pString .= TABLE::trStart();
		$pString .= TABLE::td($this->common->keywordSeparator());
		$pString .= TABLE::td($this->common->titleSubtitleSeparator());
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		$pString .= MISC::br() . "&nbsp;" . MISC::br();
		$pString .= MISC::p(FORM::formSubmit(), FALSE, "right");
		return $pString;
	}
}
?>
